package cappuccino.couldbook.base;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import cappuccino.couldbook.R;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.ui.AddnumberActivity;
import cappuccino.couldbook.ui.ApplicantActivity;
import cappuccino.couldbook.ui.CreateGroupActivity;
import cappuccino.couldbook.ui.GroupsActivity;
import cappuccino.couldbook.ui.LoginActivity;
import cappuccino.couldbook.ui.QueryNumberActivity;
import cappuccino.couldbook.ui.RegisterActivity;
import cappuccino.couldbook.ui.SearchActivity;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public abstract class BaseActivity extends AppCompatActivity{
    static public String TAG = "BaseActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG,"onCreate");
        super.onCreate(savedInstanceState);
        init();
        bindView();
        bindTitle();

    }

    protected void showBack(){
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onStart() {
        Log.v(TAG,"onStart");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.v(TAG,"onRestart");
        super.onRestart();

    }

    @Override
    protected void onResume() {
        Log.v(TAG,"onResume");

        super.onResume();
    }

    abstract protected void bindView();
    abstract protected void init();

    private Toolbar toolbar;

    public void bindTitle(){
        if(toolbar == null){
            toolbar  = (Toolbar) findViewById(R.id.toolbar);
            if(toolbar != null){
                setSupportActionBar(toolbar);
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BaseActivity.this.finish();
                    }
                });
            }
        }

    }

    public void setTitle(String title){
        if(toolbar!=null)
            getSupportActionBar().setTitle(title);
    }

    protected Toolbar getToolbar(){
        return toolbar;
    }
    protected void setToolbar(Toolbar toolbar){
        this.toolbar = toolbar;
    }
    protected boolean toLogin(){
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        return true;
    }

    protected boolean toRegister(){
        Intent intent = new Intent(this,RegisterActivity.class);
        startActivity(intent);
        return true;
    }
    protected boolean toAdd(){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else {
            Intent intent = new Intent(this, AddnumberActivity.class);
            startActivity(intent);
            return true;
        }
    }
    protected boolean toQuery(){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else{
            Intent intent = new Intent(this,QueryNumberActivity.class);
            intent.putExtra("userId", id);
            startActivity(intent);
            return true;
        }
    }
    protected boolean toGroups(){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else {
            Intent intent = new Intent(this, GroupsActivity.class);
            startActivity(intent);
            return true;
        }
    }
    protected boolean toGroups(String name){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else {
            Intent intent = new Intent(this, SearchActivity.class);
            intent.putExtra("group_name",name);
            startActivity(intent);
            return true;
        }
    }
    protected boolean toApplicant(){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else {
            Intent intent = new Intent(this, ApplicantActivity.class);
            startActivity(intent);
            return true;
        }
    }

    protected boolean toCreate(){
        AccountM accountM = new AccountM(this);
        int id = accountM.getAccout();
        if(id == -1)
        {
            Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
            return false;
        }else {
            Intent intent = new Intent(this, CreateGroupActivity.class);
            startActivity(intent);
            return true;
        }
    }
}
